package cn.iocoder.yudao.module.scenic.controller.admin.trip;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scenic.controller.admin.trip.vo.*;
import cn.iocoder.yudao.module.scenic.dal.dataobject.trip.TripDO;
import cn.iocoder.yudao.module.scenic.service.trip.TripService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 线路")
@RestController
@RequestMapping("/scenic/trip")
@Validated
public class TripController {

    @Resource
    private TripService tripService;

    @PostMapping("/create")
    @Operation(summary = "创建线路")
    @PreAuthorize("@ss.hasPermission('road:trip:create')")
    public CommonResult<Long> createTrip(@Valid @RequestBody TripSaveReqVO createReqVO) {
        return success(tripService.createTrip(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新线路")
    @PreAuthorize("@ss.hasPermission('road:trip:update')")
    public CommonResult<Boolean> updateTrip(@Valid @RequestBody TripSaveReqVO updateReqVO) {
        tripService.updateTrip(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除线路")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('road:trip:delete')")
    public CommonResult<Boolean> deleteTrip(@RequestParam("id") Long id) {
        tripService.deleteTrip(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得线路")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('road:trip:query')")
    public CommonResult<TripRespVO> getTrip(@RequestParam("id") Long id) {
        TripRespVO trip = tripService.getTrip(id);
        return success(trip);
    }

    @GetMapping("/page")
    @Operation(summary = "获得线路分页")
    @PreAuthorize("@ss.hasPermission('road:trip:query')")
    public CommonResult<PageResult<TripRespVO>> getTripPage(@Valid TripPageReqVO pageReqVO) {
        PageResult<TripDO> pageResult = tripService.getTripPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TripRespVO.class));
    }

    @PostMapping("/trip_page_by_location")
    @Operation(summary = "根据省市区获得线路分页")
    @PreAuthorize("@ss.hasPermission('road:trip:query')")
    public CommonResult<PageResult<TripPageByLocationResVO>> tripPageByLocation(@RequestBody @Valid TripPageByLocationReqVO pageReqVO) {
        PageResult<TripPageByLocationResVO> pageResult = tripService.tripPageByLocation(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出线路 Excel")
    @PreAuthorize("@ss.hasPermission('road:trip:export')")
    @OperateLog(type = EXPORT)
    public void exportTripExcel(@Valid TripPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TripDO> list = tripService.getTripPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "线路.xls", "数据", TripRespVO.class,
                        BeanUtils.toBean(list, TripRespVO.class));
    }

}